package com.example.system.controller;

import com.example.system.entity.Params;
import com.example.system.entity.User;
import com.example.system.common.Result;
import com.example.system.exception.CustomException;
import com.example.system.service.impl.UserServiceImpl;
import com.github.pagehelper.PageInfo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/user")
@MapperScan("com.*.mapper")
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        User loginUser;
        try {
            loginUser = userService.login(user);
        } catch (CustomException e) {
            return Result.error(e.getMsg());
        }
        return Result.success(loginUser);
    }

    // 注册
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        user.setUserType(1);
        try {
            userService.add(user);
        } catch (CustomException e) {
            return Result.error(e.getMsg());
        }
        return Result.success();
    }

    // 注册管理员
    @PostMapping("/register/admin")
    public Result registerAdmin(@RequestBody User user) {
        user.setUserType(0);
        try {
            userService.add(user);
        } catch (CustomException e) {
            return Result.error(e.getMsg());
        }
        return Result.success();
    }

    //增删改查
    //新增 & 修改
    // 新增或修改用户
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        if (user.getId() == null) {
            try {
                userService.add(user);
            } catch (CustomException e) {
                return Result.error(e.getMsg());
            }
        } else {
            try {
                userService.update(user);
            } catch (CustomException e) {
                return Result.error(e.getMsg());
            }
        }
        return Result.success();
    }

    // 新增家庭成员
    @PostMapping("/save/familyMember/{curUserID}/{familyMemberID}")
    public Result addNewFamilyMember(@PathVariable Integer curUserID, @PathVariable Integer familyMemberID) {
        try {
            userService.addNewFamilyMember(curUserID, familyMemberID);
        } catch (CustomException e) {
            return Result.error(e.getMsg());
        }
        return Result.success();
    }

    //删除
    /*@PathVariable注解用于将URL中的路径变量绑定到方法参数上。
    在这个例子中，@PathVariable Integer id表示
    将URL中的id路径变量绑定到id参数上。
    当请求的URL为/{id}时，@PathVariable Integer id会将
    URL中的id值赋给id参数。
    例如，如果请求的URL为/1，则id参数的值将为1。
    */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        Integer deleteRes = userService.deleteById(id);
        return Result.success(deleteRes);
    }

    @DeleteMapping("/delete/familyMember/{curUserID}/{familyMemberID}")
    public Result deleteFamilyMember(@PathVariable Integer curUserID, @PathVariable Integer familyMemberID) {
        try {
            userService.deleteFamilyMember(curUserID, familyMemberID);
        } catch (CustomException e) {
            return Result.error(e.getMsg());
        }
        return Result.success();
    }

    //根据用户名搜索用户页
    @GetMapping("/search/page")
    public Result findPageByUserName(Params params) {
        PageInfo<User> info;
        try {
            info = userService.findPageByUserName(params);
        } catch (CustomException e) {
            return Result.error(e.getMsg());
        }
        return Result.success(info);
    }

    // 根据用户ID查用户信息
    @GetMapping("/search/self/{id}")
    public Result findSelfInfoByUserID(@PathVariable Integer id) {
        User curUser = userService.findSelfInfoByUserID(id);
        if (curUser == null) {
            return Result.error("当前用户不存在");
        }
        return Result.success(curUser);
    }

    // 查询当前用户的所有家庭成员
    @GetMapping("/search/familyMember/{id}")
    public Result findFamilyMemberByUserID(@PathVariable Integer id) {
        User curUser = userService.findSelfInfoByUserID(id);
        if (curUser == null) {
            return Result.error("当前用户不存在");
        }
        List<User> familyMember = userService.findFamilyMemberByUserID(id);
        if (familyMember == null || familyMember.isEmpty()) {
            return Result.error("当前用户无家庭成员");
        }
        return Result.success(familyMember);
    }

    @GetMapping("/search/familyMember/{userId}/{familyMemberId}")
    public Result findFamilyMemberByUserIdAndFamilyMemberId(@PathVariable Integer userId, @PathVariable Integer familyMemberId) {
        List<User> userList;
        try {
            userList = userService.findFamilyMemberByUserIdAndFamilyMemberId(userId, familyMemberId);
        } catch (CustomException e) {
            return Result.error(e.getMsg());
        }
        return Result.success(userList);
    }
}
